# -*- coding: utf-8 -*-
from app import db, login_manager, bcrypt

import datetime
import re


class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    content = db.Column(db.Text, nullable=False)
    keyword = db.Column(db.String(100), nullable=False)
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now)
    published = db.Column(db.Boolean, nullable=False, default=False)
    read_count = db.Column(db.Integer, nullable=False, default=0)
    comment_count = db.Column(db.Integer, nullable=False, default=0)
    comments = db.relationship('Comment', backref='post', lazy='dynamic')

    def description(self):
        re_style = re.compile(r'<\s*style[^>]*>[^<]*<\s*/\s*style\s*>', re.I)
        re_image = re.compile(r'<\s*img/>')
        re_h = re.compile(r'</?\w+[^>]*>')
        re_ref = re.compile('&[a-zA-Z]+;')
        s = re_style.sub('', self.content)
        s = re_image.sub('', s)
        s = re_h.sub('', s)
        s = re_ref.sub('', s)
        return s[0:100]

    def thumb(self):
        re_style = re.compile('src="([^"]*)"')
        r = re_style.findall(self.content)
        if len(r) > 0:
            return re_style.findall(self.content)[0] + '?imageMogr2/thumbnail/80x'
        else:
            return '/static/images/favicon.ico' # default images


class Reader(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(50), nullable=False)
    description = db.Column(db.String(200), nullable=False)
    icon_name = db.Column(db.String(100))
    icon_url = db.Column(db.String(500))
    media_type = db.Column(db.Boolean, nullable=False, default=False)
    media_url = db.Column(db.String(500), nullable=False)
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(64), unique=True)
    password_hash = db.Column(db.String(255))
    name = db.Column(db.String(64))
    slug = db.Column(db.String(64), unique=True)
    active = db.Column(db.Boolean, default=True)
    created_timestamp = db.Column(db.DateTime, default=datetime.datetime.now)

    def get_id(self):
        return self.id

    def is_authenticated(self):
        return True

    def is_active(self):
        return self.active

    def is_anonymous(self):
        return False

    @staticmethod
    def make_password(plaintext):
        return bcrypt.generate_password_hash(plaintext)

    def check_password(self, raw_password):
        return bcrypt.check_password_hash(self.password_hash, raw_password)

    @classmethod
    def create(cls, email, password, **kwargs):
        return User(email=email, password_hash=User.make_password(password), **kwargs)

    @staticmethod
    def authenticate(email, password):
        user = User.query.filter(User.email == email).first()
        if user and user.check_password(password):
            return user
        return False


@login_manager.user_loader
def _user_loader(user_id):
    return User.query.get(int(user_id))


class Image(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    key = db.Column(db.String(100))
    ext = db.Column(db.String(10))
    create_time = db.Column(db.DateTime, default=datetime.datetime.now())


class Comment(db.Model):
    STATUS_DRAFT = 0
    STATUS_APPROVED = 1

    id = db.Column(db.Integer, primary_key=True)
    post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
    email = db.Column(db.String(100), nullable=False)
    body = db.Column(db.String(500), nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.datetime.now())
    status = db.Column(db.Integer, default=STATUS_DRAFT)  # 0 for draft, 1 for approved


class Note(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    body = db.Column(db.String(200), nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.datetime.now())
    praised = db.Column(db.Integer, default=0)
